<script setup lang="ts">
import { ref, reactive, watch, onMounted, onUnmounted, nextTick } from "vue";
import type { ComponentSize, FormInstance, FormRules } from "element-plus";
import ReTable from "@/components/ReTable/index.vue";
import ReHeaderSearch from "@/components/ReHeaderSearch/index.vue";
import ReNewDialog from "@/components/ReNewDialog/index.vue";
import { useSettingStoreHook } from "@/store/modules/settings";
import { ElMessage, ElMessageBox } from "element-plus";
import type { Action } from "element-plus";
defineOptions({
  name: "userCouponList"
});
interface ValueType {
  val: {
    buttonType: string;
  };
  row: object;
}
interface CouponType {
  coupon_id: string;
  user_id: string;
  memo: string;
}
const pureSetting = useSettingStoreHook();
const dialogViewVisible = ref(false);
const deleteModal = () => {
  ElMessageBox.confirm("确定要删除选中的优惠券吗?", "提示", {
    distinguishCancelAndClose: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消"
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "删除成功"
      });
    })
    .catch((action: Action) => {
      ElMessage({ type: "info", message: "取消删除" });
    });
};
watch(
  () => pureSetting.buttonInfo,
  (newValue, oldValue) => {
    if (Object.keys(newValue).length !== 0) {
      let value = newValue as ValueType;
      switch (value.val.buttonType) {
        case "delete":
          deleteModal();
          break;
      }
    }
  },
  {
    immediate: true,
    deep: true
  }
);
const handleGetDeleteIds = (deleteIds: string[]) => {
  deleteModal();
};
const couponForm = reactive<CouponType>({
  coupon_id: "",
  user_id: "",
  memo: ""
});
const couponOption = ref<any[]>([
  {
    label: "10元",
    value: "1"
  },
  {
    label: "20元",
    value: "2"
  }
]);
const handleGetCreateData = (data: any) => {
  dialogViewVisible.value = true;
};
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<CouponType>>({
  coupon_id: [{ required: true, message: "请选择优惠券", trigger: "blur" }],
  user_id: [{ required: true, message: "请输入用户ID", trigger: "blur" }]
});
const handleCloseDialog = () => {
  console.log("关闭弹窗");
  resetForm(ruleFormRef.value);
};
const handleConfirmDialog = () => {
  console.log("确认弹窗");
  submitForm(ruleFormRef.value);
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      resetForm(formEl);
    } else {
      console.log("error submit!", fields);
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogViewVisible.value = false;
};
</script>
<template>
  <div class="user-coupon-list-container">
    <el-card shadow="hover" class="mb-2">
      <ReHeaderSearch tableType="userCouponList" />
    </el-card>
    <ReTable
      type="userCouponList"
      create-btn-text="添加用户优惠券"
      delete-btn-text="删除用户优惠券"
      class="content-body"
      @delete="handleGetDeleteIds"
      @create="handleGetCreateData"
    />
    <ReNewDialog
      :visible="dialogViewVisible"
      dialogTitle="给用户发优惠券"
      @close="handleCloseDialog"
      @confirm="handleConfirmDialog"
    >
      <template #content>
        <el-form
          ref="ruleFormRef"
          :model="couponForm"
          :rules="rules"
          label-width="auto"
        >
          <el-form-item label="选择优惠券" prop="coupon_id">
            <el-select
              v-model="couponForm.coupon_id"
              placeholder="请选择优惠券"
            >
              <el-option
                v-for="item in couponOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发放用户ID" prop="user_id">
            <el-input
              v-model="couponForm.user_id"
              type="number"
              placeholder="请输入用户ID"
            />
          </el-form-item>
          <el-form-item label="操作备注">
            <el-input
              v-model="couponForm.user_id"
              type="textarea"
              placeholder="请输入操作备注"
            />
          </el-form-item>
        </el-form>
      </template>
    </ReNewDialog>
  </div>
</template>
